//
//  Province.m
//  Utils
//
//  Created by enghou on 2018/4/21.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "Province.h"

@implementation Province
-(instancetype)initWithSource:(NSDictionary *)source{
    self = [super initWithSource:source];
    if (self) {
        self.name = [source.allKeys firstObject];
        NSDictionary *target = [source objectForKey:self.name];
        self.code = [self contentForKey:@"val" withSource:target];
        NSMutableArray *citys = [NSMutableArray array];
        [target.allKeys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL * _Nonnull stop) {
            if (![key isEqualToString:@"val"]) {
                NSDictionary *element = [target objectForKey:key];
                for (NSString *key in element) {
                    NSDictionary *city = [element objectForKey:key];
                    City *c = [[City alloc]init];
                    c.name = key;
                    c.code = city[@"val"];
                    NSDictionary *countrys = [city objectForKey:@"items"];
                    NSMutableArray *cs = [NSMutableArray array];
                    for (NSString *k in countrys.allKeys) {
                        Country *co = [[Country alloc]init];
                        co.name = k;
                        co.code = [countrys objectForKey:k];
                        [cs addObject:co];
                    }
                    c.countrys = cs;
                    [citys addObject:c];
                }
                
            }
        }];
        self.citys = citys;
    }
    return self;
}
@end
